<template>
    <div class='data-page'>
        <el-row :gutter="20">
            <el-col :span="6">
                <div class="grid-content bg-purple">
                    独生子女数量:{{ $store.getters.childNum }}
                </div>
            </el-col>
            <el-col :span="6">
                <div class="grid-content bg-purple">
                    城市户口学生数量:{{ $store.getters.accountNum }}
                </div>
            </el-col>
            <el-col :span="6">
                <div class="grid-content bg-purple">
                    已成年学生数量:{{ $store.getters.ageNum }}
                </div>
            </el-col>
            <el-col :span="6">
                <div class="grid-content bg-purple">
                    已接种新冠疫苗人数:{{ $store.getters.vaccinationsNum }}
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-button type="success" @click="addForm">新增学生</el-button>
            <el-button type="warning">删除选中</el-button>
            <el-button type="danger">删除全部</el-button>
        </el-row>
        <el-table ref="multipleTable" :data="$store.state.tableData" empty-text="" tooltip-effect="dark"
            style="width: 100%" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" header-align="center" align="center">
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="120" header-align="center" align="center">
            </el-table-column>
            <el-table-column prop="age" label="年龄" width="120" header-align="center" align="center">
            </el-table-column>
            <el-table-column prop="child" label="是否独生" header-align="center" show-overflow-tooltip align="center">
            </el-table-column>
            <el-table-column prop="account" label="户口" header-align="center" show-overflow-tooltip align="center">
            </el-table-column>
            <el-table-column prop="phone" label="手机" header-align="center" show-overflow-tooltip align="center">
            </el-table-column>
            <el-table-column prop="identity" label="身份证" header-align="center" show-overflow-tooltip align="center">
            </el-table-column>
            <el-table-column prop="address" label="地址" header-align="center" show-overflow-tooltip align="center">
            </el-table-column>
            <el-table-column prop="vaccinations" label="新冠疫苗" header-align="center" show-overflow-tooltip
                align="center">
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="froms" v-if="$store.state.showForm">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <h1>学生信息</h1>
                <el-form-item label="名字" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="ruleForm.age"></el-input>
                </el-form-item>
                <el-form-item label="地址" required prop="address">
                    <el-input v-model="ruleForm.address"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="phone">
                    <el-input v-model="ruleForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="身份证" prop="identity">
                    <el-input v-model="ruleForm.identity"></el-input>
                </el-form-item>
                <el-form-item label="是否独生" prop="child">
                    <el-radio-group v-model="ruleForm.child">
                        <el-radio label="独生子女"></el-radio>
                        <el-radio label="非独生"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否接种" prop="vaccinations">
                    <el-radio-group v-model="ruleForm.vaccinations">
                        <el-radio label="未接种"></el-radio>
                        <el-radio label="已接种"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="户口类型" prop="address">
                    <el-select v-model="ruleForm.account" placeholder="户口所在地">
                        <el-option label="城市户口" value="城市户口"></el-option>
                        <el-option label="农村户口" value="农村户口"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item align="right">
                    <el-button @click="submitForm('ruleForm')">取消</el-button>
                    <el-button type="primary" @click="resetForm('ruleForm')">确认</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    name: 'data-page',
    /**
    * 定义组件的data函数，用于声明组件内部的状态。
    * 
    * @returns {Object} 返回一个对象，其中包含组件内部的状态。这些状态将在组件的生命周期内被管理和使用。
    */
    data() {
        return {
            multipleSelection: [],
            addFrom: {
                child: '独生子女',//是否独生
                vaccinations: '未接种',//是否接种新冠疫苗
            },
            ruleForm: {
                // name: '苏落',
                // age: 19,
                // child: '独生子女',//是否独生
                // account: '农村户口',//户口
                // phone: 17752750628,//手机号码
                // identity: '431022200022229292',//身份证号码
                // address: '上海市路 1518 弄',//地址
                // vaccinations: '未接种',//是否接种新冠疫苗
            },
            rules: {
                name: [
                    { required: true, message: '请输入学生名称', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                ],
                age: [
                    { required: true, message: '请输入学生年龄', trigger: 'blur' },
                    { min: 1, max: 2, message: '请输入正确年龄', trigger: 'blur' }
                ],
                address: [
                    { required: true, message: '请输入地址', trigger: 'blur' },
                ],
                phone: [
                    { required: true, message: '输入手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '请输入正确格式', trigger: 'blur' },
                ],
                identity: [
                    { required: true, message: '请输入身份证号', trigger: 'blur' },
                    { min: 18, max: 18, message: '请输入正确格式', trigger: 'blur' },
                ],
                child: [
                    { type: 'string', message: '请至少选择一个子女类型', trigger: 'change' }
                ],
                vaccinations: [
                    { message: '请选择是否接种疫苗', trigger: 'change' }
                ],
                account: [
                    { required: true, message: '至少选择一种户口类型', trigger: 'blur' },
                ]
            }
        }
    },
    methods: {
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleRowSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleEdit(index, row) {
            this.$store.commit('showForm', true);
            this.ruleForm = row;
            console.log(index, row);
        },
        handleDelete(index, row) {
            console.log(index, row);
        },

        submitForm(formName) {
            this.$store.commit('showForm', false);
            console.log('submitForm', formName);
        },
        resetForm(formName) {
            this.$store.commit('showForm', false);
            this.$store.commit('addForm', this.ruleForm);
            this.ruleForm = this.addFrom;
            console.log('添加:', this.ruleForm, this.addFrom);
            console.log('formName', formName, this.$refs);
        },
        addForm() {
            this.$store.commit('showForm', true);
            console.log('添加菘权工:', this.ruleForm, this.addFrom);
            this.ruleForm = this.addFrom;
            console.log('添加菘权工:', this.ruleForm, this.addFrom);

        }
    }
}
</script>

<!-- scoped,样式隔离，只作用于当前组建 -->
<style scoped>
.data-page {
    position: relative;
    padding: 20px;
}

.froms {
    position: absolute;
    top: 0;
    left: 0;
    height: 100vh;
    width: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1;
}

.demo-ruleForm {
    width: 40%;
    margin: 0 auto;
    background: #fff;
    padding: 30px;
    margin-top: 30px;
    border-radius: 5px;
}

.el-row {
    margin-bottom: 20px;
}

.el-row:last-child {
    margin-bottom: 0;
}

.el-col {
    border-radius: 4px;
}

.bg-purple-dark {
    background: #99a9bf;
}

.bg-purple {
    /* background: #d3dce6; */
    height: 20px;
    line-height: 38px;
    font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
    font-size: 14px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    padding: 20px;
}

.bg-purple-light {
    background: #e5e9f2;
}

.grid-content {
    border-radius: 4px;
    min-height: 36px;
}

.row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
}
</style>